
local colors = {0xffffffff,0xff00ff00,0xff00ccff,0xffca42fd,0xffffff00,0xffff6800,0xffff0000,}
local util = require 'util.color'
function item_color(lv)
    local color = colors[lv] or colors[#colors]
    local color_text = util.text(color)
    return color,color_text
end

function load_item_state(hero,state,add)
    if hero==nil or state==nil or not hero:is_type('英雄') then
        return
    end
    for _,da in ipairs(state) do
        local key = da[1]
        local num = da[2]
        hero:add(key,num*add)
    end
end


ac.game:event '单位-添加物品属性'(function(_,hero,item)
    load_item_state(hero,item.state,1)
    item.abi_obj = {}
    for i,abi in ipairs(item.abi_list or {}) do
        item.abi_obj[i] = hero:add_skill(abi,'物品',nil,{item = item})
    end
end)

ac.game:event '单位-移除物品属性'(function(_,hero,item)
    load_item_state(hero,item.state,-1)
    for i,temp in ipairs(item.abi_obj or {}) do
        temp:remove()
    end
end)





--整理装备位置
function collate_item(player,item)
    local point = player.item_point
    local lv = item.item_level or 0
    local a,b = int_grouping(lv,3)
    local p = point + {(b-2)*150,(-a+2)*150}
    item:set_point(p)
end

--英雄获得装备
function player_add_item(player,item)
    local point = player.item_point
    local hero = player.hero
	if type(item)=='string' then
		item = ac.item.create(item,point)
	end
    item.owner_player = player
    if item.owner~=hero  then
        if hero and hero:is_alive() and #hero:get_items()<6 then
            item:give(hero)
            return item
        end
        collate_item(player,item)
    end


	return item
end
